﻿using System;
using System.IO;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Navigation;

namespace EAppointmentsWPF
{
	public partial class TimelineWeek
	{
        int[,] arr1 = new int[24, 6];
        int[,] arr2 = new int[24, 6];
        int[,] arr3 = new int[24, 6];
        int[,] arr4 = new int[24, 6];
        int[,] arr5 = new int[24, 6];
        int[,] arr6 = new int[24, 6];
        int[,] arr7 = new int[24, 6];
		public TimelineWeek()
		{
			this.InitializeComponent();

			// Insert code required on object creation below this point.
		}

        private void TimelineWeek1_Loaded(object sender, RoutedEventArgs e)
        {   
           
         /*   DateTime dateTime = StringToDateTime(strDate);
            add(dateTime, arr1, arr2, arr3, arr4, arr5, arr6, arr7);
            for (int i = 2; i <= 7; i++)
            {
                dateTime = dateTime.AddDays(1);
                add(dateTime, arr1, arr2, arr3, arr4, arr5, arr6, arr7);
            }*/
           
        }
        void initArrays()
        {
            /**Mon Area**/
            arr1[0, 0] = 1;
            arr1[0, 1] = 0;
            for (int i = 1; i < 24; i++)
            {
                arr1[i, 0] = 1; //Starting Width
                arr1[i, 1] = arr1[i - 1, 1] + 27; //Height
            }
            /************/
            /**Tue Area**/
            arr2[0, 0] = 80;
            arr2[0, 1] = 0;
            for (int i = 1; i < 24; i++)
            {
                arr2[i, 0] = 80; //Starting Width
                arr2[i, 1] = arr2[i - 1, 1] + 27; //Height
            }
            /************/
            /**Wed Area**/
            arr3[0, 0] = 155;
            arr3[0, 1] = 0;
            for (int i = 1; i < 24; i++)
            {
                arr3[i, 0] = 155; //Starting Width
                arr3[i, 1] = arr3[i - 1, 1] + 27; //Height
            }
            /************/
            /**Thu Area**/
            arr4[0, 0] = 230;
            arr4[0, 1] = 0;
            for (int i = 1; i < 24; i++)
            {
                arr4[i, 0] = 230; //Starting Width
                arr4[i, 1] = arr4[i - 1, 1] + 27; //Height
            }
            /************/
            /**Fri Area**/
            arr5[0, 0] = 306;
            arr5[0, 1] = 0;
            for (int i = 1; i < 24; i++)
            {
                arr5[i, 0] = 306; //Starting Width
                arr5[i, 1] = arr5[i - 1, 1] + 27; //Height
            }
            /************/
            /**Sat Area**/
            arr6[0, 0] = 380;
            arr6[0, 1] = 0;
            for (int i = 1; i < 24; i++)
            {
                arr6[i, 0] = 380; //Starting Width
                arr6[i, 1] = arr6[i - 1, 1] + 27; //Height
            }
            /************/
            /**Sun Area**/
            arr7[0, 0] =457;
            arr7[0, 1] = 0;
            for (int i = 1; i < 24; i++)
            {
                arr7[i, 0] = 457; //Starting Width
                arr7[i, 1] = arr7[i - 1, 1] + 27; //Height
            }
            /************/
        }
        void add(DateTime dateTime, int[,] arr1, int[,] arr2, int[,] arr3, int[,] arr4, int[,] arr5, int[,] arr6, int[,] arr7)
        {
            //Find Appointments for date in dateTime here
            switch (checkIndex(dateTime))
            {
                case 1://Use Array1 
                       txtMonDate.Text = dateTime.Day.ToString()+" "+dateTime.Date.ToString("dd-MMM-yyyy").Substring(3,3);
                       //addGlyph(arr1, IndexSearch("4:30 pm"), "Pending");
                       //addGlyph(arr1, IndexSearch("5:57 pm"), "Booked");
                       addGlyph(arr1, IndexSearch("9:10 am"), "Approved");
                       //addGlyph(arr1, IndexSearch("8:30 am"), "Rejected");
                       //addGlyph(arr1, IndexSearch("6:30 pm"), "Canceled");     
                       break;
                case 2://Use Array2
                       txtTueDate.Text = dateTime.Day.ToString() + " " + dateTime.Date.ToString("dd-MMM-yyyy").Substring(3, 3);
                       addGlyph(arr2, IndexSearch("6:30 pm"), "Pending");
                       //addGlyph(arr2, IndexSearch("4:57 pm"), "Booked");
                       //addGlyph(arr2, IndexSearch("5:70 pm"), "Approved");
                       addGlyph(arr2, IndexSearch("8:30 pm"), "Rejected");
                       //addGlyph(arr2, IndexSearch("9:30 pm"), "Canceled");
                       break;
                case 3://Use Array3
                        txtWedDate.Text = dateTime.Day.ToString() + " " + dateTime.Date.ToString("dd-MMM-yyyy").Substring(3, 3);
                       //addGlyph(arr3, IndexSearch("7:30 am"), "Pending");
                       addGlyph(arr3, IndexSearch("11:57 am"), "Booked");
                       //addGlyph(arr3, IndexSearch("11:10 am"), "Approved");
                       //addGlyph(arr3, IndexSearch("12:30 am"), "Rejected");
                       //addGlyph(arr3, IndexSearch("7:00 pm"), "Canceled");
                       break;
                case 4://Use Array4
                       txtThuDate.Text = dateTime.Day.ToString() + " " + dateTime.Date.ToString("dd-MMM-yyyy").Substring(3, 3);
                       //addGlyph(arr4, IndexSearch("10:30 am"), "Pending");
                       //addGlyph(arr4, IndexSearch("4:57 pm"), "Booked");
                       //addGlyph(arr4, IndexSearch("4:10 pm"), "Approved");
                       //addGlyph(arr4, IndexSearch("4:30 pm"), "Rejected");
                       //addGlyph(arr4, IndexSearch("6:30 am"), "Canceled");
                       break;
                case 5://Use Array5
                       txtFriDate.Text = dateTime.Day.ToString() + " " + dateTime.Date.ToString("dd-MMM-yyyy").Substring(3, 3);
                       //addGlyph(arr5, IndexSearch("8:30 am"), "Pending");
                       //addGlyph(arr5, IndexSearch("4:57 pm"), "Booked");
                       //addGlyph(arr5, IndexSearch("6:10 am"), "Approved");
                       //addGlyph(arr5, IndexSearch("4:30 pm"), "Rejected");
                       addGlyph(arr5, IndexSearch("9:30 pm"), "Canceled");
                       break;
                case 6: //Use Array6
                       txtSatDate.Text = dateTime.Day.ToString() + " " + dateTime.Date.ToString("dd-MMM-yyyy").Substring(3, 3);
                       //addGlyph(arr6, IndexSearch("3:30 pm"), "Pending");
                       //addGlyph(arr6, IndexSearch("1:57 pm"), "Booked");
                       addGlyph(arr6, IndexSearch("1:10 pm"), "Approved");
                       //addGlyph(arr6, IndexSearch("3:30 pm"), "Rejected");
                       //addGlyph(arr6, IndexSearch("4:30 am"), "Canceled");
                       break;
                case 7://Use Array7
                       txtSunDate.Text = dateTime.Day.ToString() + " " + dateTime.Date.ToString("dd-MMM-yyyy").Substring(3, 3);
                       //addGlyph(arr7, IndexSearch("1:30 pm"), "Pending");
                       //addGlyph(arr7, IndexSearch("4:57 pm"), "Booked");
                       //addGlyph(arr7, IndexSearch("2:10 pm"), "Approved");
                       //addGlyph(arr7, IndexSearch("7:30 pm"), "Rejected");
                       //addGlyph(arr7, IndexSearch("5:30 pm"), "Canceled");
                       break;
            }
        }
        int checkIndex(DateTime dt)
        {
            string DayOfWeek=dt.DayOfWeek.ToString();
            if (DayOfWeek.ToLower().Trim() == "monday")
            {
                return 1;
            }
            else if (DayOfWeek.ToLower().Trim() == "tuesday")
            {
                return 2;
            }
            else if (DayOfWeek.ToLower().Trim() == "wednesday")
            {
                return 3;
            }
            else if (DayOfWeek.ToLower().Trim() == "thursday")
            {
                return 4;
            }
            else if (DayOfWeek.ToLower().Trim() == "friday")
            {
                return 5;
            }
            else if (DayOfWeek.ToLower().Trim() == "saturday")
            {
                return 6;
            }
            else //sunday
            {
                return 7;
            }
        }
        void addGlyph(int[,] arr, int index, string statusValue)
        {
            ContentControl cn = new ContentControl();
            cn.Width =19;
            cn.Height = 27;
            cn.Style = this.Resources["BookedGlyphStyle"] as Style;
            cn.SetValue(Canvas.LeftProperty, (double)arr[index, 0]);
            arr[index, 0] += 12;
            cn.SetValue(Canvas.TopProperty, (double)arr[index, 1]);
            switch (statusValue)
            {
                case "Approved":
                    cn.Style = this.Resources["ApprovedGlyphStyle"] as Style;
                    break;

                case "Pending":
                    cn.Style = this.Resources["PendingGlyphStyle"] as Style;
                    break;

                case "Booked":
                    cn.Style = this.Resources["BookedGlyphStyle"] as Style;
                    break;
                case "Rejected":
                    cn.Style = this.Resources["RejectedGlyphStyle"] as Style;
                    break;
                case "Canceled":
                    cn.Style = this.Resources["CanceledGlyphStyle"] as Style;
                    break;
            }
            Canvas1.Children.Add(cn);
        }
        int IndexSearch(string time)
        {
            string[] str = new string[2];
            str = time.Split(':');
            int index = int.Parse(str[0]);
            if (time.Contains("p"))
            {
                if(index!=12)
                    index += 12;
            }
            else
            {
                if (index == 12)
                    index = 0;
            }
            return index;
        }
        public static DateTime StringToDateTime(string strDate)
        {
            string[] str = new string[3];
            str = strDate.Split('/');
            DateTime date = new DateTime(Int32.Parse(str[2]), Int32.Parse(str[0]), Int32.Parse(str[1]));
            return date;
        }

        private void txtBox_TextChanged(object sender, TextChangedEventArgs e)
        {
            if (txtBox.Text != "TextBox")
            {
                Canvas1.Children.Clear();
                initArrays();
                DateTime dateTime = StringToDateTime(txtBox.Text.Substring(0,10));
                add(dateTime, arr1, arr2, arr3, arr4, arr5, arr6, arr7);
                for (int i = 2; i <= 7; i++)
                {
                    dateTime = dateTime.AddDays(1);
                    add(dateTime, arr1, arr2, arr3, arr4, arr5, arr6, arr7);
                }
            }
        }

       
	}
}